package com.person.webdemoproject2.controller;



import com.person.webdemoproject2.pojo.Result;
import com.person.webdemoproject2.pojo.Text;
import com.person.webdemoproject2.pojo.User;
import com.person.webdemoproject2.service.SetAccountService;
import com.person.webdemoproject2.service.UploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/user")
public class SetAccountController {

    @Autowired
    SetAccountService setAccountService;
    @PostMapping("/set")
    public Result SetAccount(@RequestBody User user) {
        System.out.println(user.toString());
       log.info("用户注册 {}",user.getUsername());

        if (setAccountService.SetAccount(user)){
            return Result.success(user.getUsername());
        }
        return Result.error("您的用户名已存在");
    }
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        log.info("用户登录 {}",user.getUsername());
        String token = setAccountService.login(user);
        if (token != null) {
            return Result.success(token);
        }
        return Result.error("用户名或密码错误");
    }
}
//    @Autowired
//    UploadService uploadService;
//    @PostMapping("/text")
//    public Result uploadtext(@RequestBody Text text, @RequestHeader String token) {
//        log.info("用户输入文本内容");
//        boolean uploadtext = uploadService.uploadtext(text, token);
//        if (uploadtext) {
//            return Result.success("发布成功");
//        }
//        return Result.error("内容为空,或需要重新登录");
//    }
//}
